﻿////写一个订单管理的控制台程序，能够实现添加订单，删除订单，查询订单（订单号，商品名，客户，订单金额）
////主要的类有Order,OrderDetail,OrderService
////订单数据可以保存在OrderService中一个List中
////在Program里面可以调用OrderService的方法完成各种订单操作
//1.使用LINQ实现各种查询功能，查询结果按照订单总金额排序返回
//2.在订单删除，修改失败时，能够产生异常并显示给客户错误信息
////3.Order和OrderDetail类需要重写Equals方法，确保添加的订单和订单明细不重复
////4.订单、订单明细、客户、货物等类添加ToString方法，用来显示订单信息
//5.OrderService提供排序方法对保存的订单进行排序。默认按照订单号排序，也可以使用Lambda表达式进行自定义


//在OrderService中添加一个Export方法，可以将所有的订单序列化为xml文件；添加一个Import方法可以从xml文件中载入订单
//对订单程序中OrderService的各个public方法添加测试用例

using OrderManager;

OrderService ser = new OrderService();

int choice = 0;
while (true)
{
    Console.WriteLine("订单管理系统");
    Console.WriteLine("1.添加订单");
    Console.WriteLine("2.修改订单");
    Console.WriteLine("3.查询订单");
    Console.WriteLine("4.删除订单");
    Console.WriteLine("5.订单排序");
    Console.WriteLine("6.订单序列化");
    Console.WriteLine("7.从xml文件中载入订单");
    Console.WriteLine("0.退出系统");

    choice = Convert.ToInt32(Console.ReadLine());

    switch (choice)
    {
        case 1:
            {
                Customer customer = new Customer();
                Console.WriteLine("Please input customer Name:");
                string cusName = Console.ReadLine();
                customer.CustomerName = cusName;

                Stuff stuff = new Stuff();
                Console.WriteLine("Please input stuff Type:");
                string stuType = Console.ReadLine();
                stuff.StuffType = stuType;
                Console.WriteLine("stuff total price");
                double stuprice = Convert.ToDouble(Console.ReadLine());
                stuff.TotalPrice = stuprice;

                Order order = new Order();
                Console.WriteLine("Please input the order ID:");
                int id = Convert.ToInt32(Console.ReadLine());
                order.Id = id;
                order.stuff = stuff;
                order.customer = customer;
                ser.AddOrder(order);
                Console.ReadKey();
                Console.Clear();
                break;
            }
        case 2:
            {
                Console.WriteLine("Please input the order Id you want to edit");
                int ordID = Convert.ToInt32(Console.ReadLine());

                //var query = from o in ser.orders where o.Id == ordID select o;
                //List<Order> ord = query.ToList();
                //if (ord.Count() > 0)
                //{

                //    Console.WriteLine("Find the target order:");
                //    foreach (Order o in ord)
                //        Console.WriteLine(o.ToString());
                //}
                //else
                //{
                //    Console.WriteLine("Order does not exist");
                //    return;
                //}

                bool flag = ser.InqOrder(ordID);
                if (!flag) break;

                Console.WriteLine("Please choose the edit item:");
                Console.WriteLine("1.order ID");
                Console.WriteLine("2.customer name");
                Console.WriteLine("3.stuff total price");
                Console.WriteLine("4.stuff type");
                int c = Convert.ToInt32(Console.ReadLine());
                switch (c)
                {
                    case 1:
                        {
                            Console.WriteLine("Please input the updated order ID:");
                            int id = Convert.ToInt32(Console.ReadLine());
                            ser.EditOrder(ser.OrderInq[0],id);
                            break;
                        }
                    case 2:
                        {
                            Console.WriteLine("Please input the customer name:");
                            string name = Console.ReadLine();
                            ser.EditOrder(ser.OrderInq[0], name, c);
                            break;
                        }
                    case 3:
                        {
                            Console.WriteLine("Please input the price:");
                            double price = Convert.ToDouble(Console.ReadLine());
                            ser.EditOrder(ser.OrderInq[0], price);
                            break;
                        }
                    case 4:
                        {
                            Console.WriteLine("Please input the stuff type:");
                            string stutype = Console.ReadLine();
                            ser.EditOrder(ser.OrderInq[0], stutype, c);
                            break;
                        }
                    default: break;

                }
                Console.ReadKey();
                Console.Clear();
                break;
            }
        case 3:
            {
                Console.WriteLine("Please choose the inquiry method");
                Console.WriteLine("1.order ID");
                Console.WriteLine("2.customer name");
                Console.WriteLine("3.stuff total price");
                Console.WriteLine("4.stuff type");
                int c = Convert.ToInt32(Console.ReadLine());
                switch (c)
                {
                    case 1:
                        {
                            Console.WriteLine("Please input the order ID:");
                            int id = Convert.ToInt32(Console.ReadLine());
                            ser.InqOrder(id);
                            break;
                        }
                    case 2:
                        {
                            Console.WriteLine("Please input the customer name:");
                            string name = Console.ReadLine();
                            ser.InqOrder(2, name);
                            break;
                        }
                    case 3:
                        {
                            Console.WriteLine("Please input the price:");
                            double price = Convert.ToDouble(Console.ReadLine());
                            ser.InqOrder(price);
                            break;
                        }
                    case 4:
                        {
                            Console.WriteLine("Please input the stuff type:");
                            string stutype = Console.ReadLine();
                            ser.InqOrder(3, stutype);
                            break;
                        }
                    default: break;

                }
                Console.ReadKey();
                Console.Clear();
                break;
            }
        case 4:
            {
                Console.WriteLine("Please choose the inquiry method");
                Console.WriteLine("1.order ID");
                Console.WriteLine("2.customer name");
                Console.WriteLine("3.stuff total price");
                Console.WriteLine("4.stuff type");
                int c = Convert.ToInt32(Console.ReadLine());
                switch (c)
                {
                    case 1:
                        {
                            Console.WriteLine("Please input the order ID:");
                            int id = Convert.ToInt32(Console.ReadLine());
                            bool flag = ser.InqOrder(id);
                            if (flag)
                            {
                                for (int i = 0; i < ser.orders.Count(); i++)
                                {
                                    Console.WriteLine("Delete? y/n");
                                    char ch = Convert.ToChar(Console.ReadLine());
                                    switch (ch)
                                    {
                                        case 'y':
                                            {
                                                ser.DelOrder(id);
                                                //Console.WriteLine(ser.DelOrder(id));
                                                break;
                                            }
                                        case 'n': break;
                                        default: break;
                                    }
                                }
                            }
                            break;
                        }
                    case 2:
                        {
                            Console.WriteLine("Please input the customer name:");
                            string name = Console.ReadLine();
                            bool flag = ser.InqOrder(2, name);
                            if (flag)
                            {
                                for (int i = 0; i < ser.orders.Count(); i++)
                                {
                                    if (ser.orders[i].customer.CustomerName == name)

                                    {
                                        Console.WriteLine(ser.orders[i].ToString());
                                        Console.WriteLine("Delete? y/n");
                                        char ch = Convert.ToChar(Console.ReadLine());
                                        switch (ch)
                                        {
                                            case 'y':
                                                {
                                                    ser.DelOrder(i);
                                                    break;

                                                }
                                            case 'n':break;
                                            default: break;
                                        }

                                    }
                                }

                            }
                            break;
                        }
                    case 3:
                        {
                            Console.WriteLine("Please input the price:");
                            double price = Convert.ToDouble(Console.ReadLine());
                            ser.InqOrder(price);
                            bool flag = ser.InqOrder(price);
                            if (flag)
                            {
                                for (int i = 0; i < ser.orders.Count(); i++)
                                {
                                    if (ser.orders[i].stuff.TotalPrice == price)

                                    {
                                        Console.WriteLine(ser.orders[i].ToString());
                                        Console.WriteLine("Delete? y/n");
                                        char ch = Convert.ToChar(Console.ReadLine());
                                        switch (ch)
                                        {
                                            case 'y':
                                                {
                                                    ser.DelOrder(i);
                                                    break;

                                                }
                                            case 'n': break;
                                            default: break;
                                        }

                                    }
                                }

                            }
                            break;
                        }
                    case 4:
                        {
                            Console.WriteLine("Please input the stuff type:");
                            string stutype = Console.ReadLine();
                            bool flag = ser.InqOrder(3, stutype);
                            if (flag)
                            {
                                for (int i = 0; i < ser.orders.Count(); i++)
                                {
                                    if (ser.orders[i].stuff.StuffType == stutype)

                                    {
                                        Console.WriteLine(ser.orders[i].ToString());
                                        Console.WriteLine("Delete? y/n");
                                        char ch = Convert.ToChar(Console.ReadLine());
                                        switch (ch)
                                        {
                                            case 'y':
                                                {
                                                    ser.DelOrder(i);
                                                    break;

                                                }
                                            case 'n': break;
                                            default: break;
                                        }

                                    }
                                }

                            }
                            break;
                        }
                    default: break;

                }
                Console.ReadKey();
                Console.Clear();
                break;
            }
        case 5:
            {
                ser.SortOrder(0);
                ser.SortOrder(1);
                Console.ReadKey();
                Console.Clear(); 
                break;
            }
        case 6:
            {
                ser.Export(); 
                Console.ReadKey();
                Console.Clear(); 
                break;
            }
        case 7:
            {
                ser.Import(); 
                Console.ReadKey();
                Console.Clear(); 
                break;
            }
        case 0: System.Environment.Exit(0); break;
        default: System.Environment.Exit(0); break;

    }
}


